package com.stellar.circle.basic.service.impl;

import com.stellar.circle.basic.dao.SPostTagDao;
import com.stellar.circle.basic.entity.SPostTag;
import com.stellar.circle.basic.service.SPostTagService;
import com.stellar.circle.constants.CircleConstants;
import com.stellar.circle.controller.admin.sPostTag.vo.HotTopicVO;
import com.stellar.circle.controller.admin.sPostTag.vo.PostTagOverviewVO;
import com.stellar.circle.controller.admin.sPostTag.vo.UsageTrend;
import com.stellar.web.core.enums.IsDeletedFlagEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 话题表(SPostTag)服务实现
 *
 * @author Jqchan
 * @since 2025-05-17 19:30:33
 */
@Service
public class SPostTagServiceImpl implements SPostTagService {

    @Resource
    private SPostTagDao sPostTagDao;

    @Override
    public SPostTag queryById(Long id) {
        return sPostTagDao.queryById(id);
    }

    @Override
    public SPostTag queryByCondition(SPostTag condition) {
        return sPostTagDao.queryByCondition(condition);
    }

    @Override
    public List<SPostTag> queryAllByCondition(SPostTag condition) {
        return sPostTagDao.queryAllByCondition(condition);
    }

    @Override
    public List<SPostTag> page(SPostTag condition, int page, int pageSize) {
        return sPostTagDao.page(condition, page, pageSize);
    }

    @Override
    public int insert(SPostTag entity) {
        return sPostTagDao.insert(entity);
    }

    @Override
    public int update(SPostTag entity) {
        return sPostTagDao.update(entity);
    }

    @Override
    public int deleteById(Long id) {
        return sPostTagDao.deleteById(id);
    }

    @Override
    public Long count(SPostTag condition) {
        return sPostTagDao.count(condition);
    }

    @Override
    public Long callUpsertPostTag(String postTag) {
        return sPostTagDao.callUpsertPostTag(postTag);
    }

    @Override
    public List<SPostTag> queryByIds(List<Long> tagIds) {
        return sPostTagDao.queryByIds(tagIds);
    }

    @Override
    public Long getOrCreateTag(String tagName) {
        // 查询现有标签
        SPostTag existingTag = sPostTagDao.selectByTagName(tagName);

        if (existingTag != null) {
            // 更新使用计数
            sPostTagDao.incrementUsageCount(existingTag.getId());
            return existingTag.getId();
        }

        // 创建新标签
        SPostTag newTag = new SPostTag();
        newTag.setPostTagName(tagName);
        newTag.setUsageCount(1);
        newTag.setIsGlobal(CircleConstants.SCHOOL_TOPIC);
        newTag.setIsPinned(CircleConstants.NOT_PINNED);
        newTag.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getVal());
        sPostTagDao.insert(newTag);
        return newTag.getId();
    }

    @Override
    public PostTagOverviewVO selectTagStats() {
        return sPostTagDao.selectTagStats();
    }

    @Override
    public PostTagOverviewVO selectTypeDistribution() {
        return sPostTagDao.selectTypeDistribution();
    }

    @Override
    public List<UsageTrend> selectRecentUsageTrend() {
        return sPostTagDao.selectRecentUsageTrend();
    }

    @Override
    public List<HotTopicVO> selectHotTopics() {
        return sPostTagDao.selectHotTopics();
    }
}
